| Conditions | 1 |
| Paths | 1 |
| Total Lines | 189 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global API */ |
||
| 25 | window.contextMenu = (function () { |
||
| 26 | 'use strict'; |
||
| 27 | var storage = new API.Storage(); |
||
| 28 | var exportContextMenu = { |
||
| 29 | setContextItems: function (logins) { |
||
| 30 | var i,f, field; |
||
| 31 | var fields = [ |
||
| 32 | {menu: 'autoFill:', field: 'autoFill', found: false}, |
||
| 33 | {field: 'username', menu: 'copy:User', found: false}, |
||
| 34 | {field: 'password', menu: 'copy:Pass', found: false}, |
||
| 35 | {field: 'url', menu: 'copy:Url', found: false}, |
||
| 36 | {field: 'totp', menu: 'copy:OTP', found: false} |
||
| 37 | ]; |
||
| 38 | API.contextMenus.removeAll(); |
||
| 39 | initMenus(); |
||
| 40 | |||
| 41 | for (i = 0; i < logins.length; i++) { |
||
| 42 | var login = logins[i]; |
||
| 43 | login.autoFill = (!login.hasOwnProperty('autoFill')) ? true : login.autoFill; |
||
| 44 | for (f = 0; f < fields.length; f++) { |
||
| 45 | field = fields[f]; |
||
| 46 | if (field.field === 'totp' && login.otp) { |
||
| 47 | login.totp = login.otp.secret; |
||
| 48 | } |
||
| 49 | if (login[field.field]) { |
||
| 50 | fields[f].found = true; |
||
| 51 | /* jshint ignore:start */ |
||
| 52 | createMenuItem(field.menu, field.menu + ':' + login.guid, login.label, (function (field, login) { |
||
| 53 | return function () { |
||
| 54 | itemClickCallback(field, login); |
||
| 55 | }; |
||
| 56 | })(field, login)); |
||
| 57 | /* jshint ignore:end */ |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | for (f = 0; f < fields.length; f++) { |
||
| 63 | field = fields[f]; |
||
| 64 | if(field.found === false){ |
||
| 65 | API.contextMenus.remove(field.menu); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | }, |
||
| 70 | addPasswordGenerator: function(){ |
||
| 71 | createMenuItem('generatePassword', 'copyGen', 'And copy to clipboard', function(){ |
||
| 72 | generatePass(function (generated_password) { |
||
| 73 | copyText(generated_password); |
||
| 74 | }); |
||
| 75 | }); |
||
| 76 | |||
| 77 | createMenuItem('generatePassword', 'fill', 'And fill fields', function(){ |
||
| 78 | generatePass(function (generated_password) { |
||
| 79 | var login = { |
||
| 80 | password: generated_password |
||
| 81 | }; |
||
| 82 | API.tabs.query({active: true, currentWindow: true}).then(function (tabs) { |
||
| 83 | API.tabs.sendMessage(tabs[0].id, {method: "enterLoginDetails", args: login}).then(function (response) { |
||
|
|
|||
| 84 | }); |
||
| 85 | }); |
||
| 86 | }); |
||
| 87 | |||
| 88 | }); |
||
| 89 | } |
||
| 90 | }; |
||
| 91 | |||
| 92 | |||
| 93 | function generatePass(cb){ |
||
| 94 | var default_settings = { |
||
| 95 | 'length': 12, |
||
| 96 | 'useUppercase': true, |
||
| 97 | 'useLowercase': true, |
||
| 98 | 'useDigits': true, |
||
| 99 | 'useSpecialChars': true, |
||
| 100 | 'minimumDigitCount': 3, |
||
| 101 | 'avoidAmbiguousCharacters': false, |
||
| 102 | 'requireEveryCharType': true |
||
| 103 | }; |
||
| 104 | storage.get('password_generator_settings').then(function (_settings) { |
||
| 105 | if (!_settings) { |
||
| 106 | _settings = default_settings; |
||
| 107 | } |
||
| 108 | /* jshint ignore:start */ |
||
| 109 | var password = generatePassword(_settings['length'], |
||
| 110 | _settings.useUppercase, |
||
| 111 | _settings.useLowercase, |
||
| 112 | _settings.useDigits, |
||
| 113 | _settings.useSpecialChars, |
||
| 114 | _settings.minimumDigitCount, |
||
| 115 | _settings.avoidAmbiguousCharacters, |
||
| 116 | _settings.requireEveryCharType); |
||
| 117 | /* jshint ignore:end */ |
||
| 118 | cb(password); |
||
| 119 | }).error(function () { |
||
| 120 | /* jshint ignore:start */ |
||
| 121 | var password = generatePassword(default_settings['length'], |
||
| 122 | default_settings.useUppercase, |
||
| 123 | default_settings.useLowercase, |
||
| 124 | default_settings.useDigits, |
||
| 125 | default_settings.useSpecialChars, |
||
| 126 | default_settings.minimumDigitCount, |
||
| 127 | default_settings.avoidAmbiguousCharacters, |
||
| 128 | default_settings.requireEveryCharType); |
||
| 129 | /* jshint ignore:end */ |
||
| 130 | cb(password); |
||
| 131 | }); |
||
| 132 | } |
||
| 133 | |||
| 134 | function initMenus() { |
||
| 135 | API.contextMenus.create({ |
||
| 136 | id: 'autoFill:', |
||
| 137 | title: 'Auto fill', |
||
| 138 | contexts: ['page'] |
||
| 139 | }); |
||
| 140 | |||
| 141 | API.contextMenus.create({ |
||
| 142 | id: 'generatePassword', |
||
| 143 | title: 'Generate password', |
||
| 144 | contexts: ['page'] |
||
| 145 | }); |
||
| 146 | |||
| 147 | API.contextMenus.create({ |
||
| 148 | id: 'copy:User', |
||
| 149 | title: 'Copy username', |
||
| 150 | contexts: ['page'] |
||
| 151 | }); |
||
| 152 | |||
| 153 | API.contextMenus.create({ |
||
| 154 | id: 'copy:Pass', |
||
| 155 | title: 'Copy password', |
||
| 156 | contexts: ['page'] |
||
| 157 | }); |
||
| 158 | |||
| 159 | |||
| 160 | API.contextMenus.create({ |
||
| 161 | id: 'copy:Url', |
||
| 162 | title: 'Copy URL', |
||
| 163 | contexts: ['page'] |
||
| 164 | }); |
||
| 165 | |||
| 166 | API.contextMenus.create({ |
||
| 167 | id: 'copy:OTP', |
||
| 168 | title: 'Copy OTP', |
||
| 169 | contexts: ['page'] |
||
| 170 | }); |
||
| 171 | exportContextMenu.addPasswordGenerator(); |
||
| 172 | } |
||
| 173 | |||
| 174 | function createMenuItem(parentId, id, label, clickcb) { |
||
| 175 | API.contextMenus.create({ |
||
| 176 | id: id, |
||
| 177 | title: label, |
||
| 178 | contexts: ["page"], |
||
| 179 | parentId: parentId, |
||
| 180 | onclick: clickcb |
||
| 181 | }); |
||
| 182 | } |
||
| 183 | |||
| 184 | function itemClickCallback(menu_action, login) { |
||
| 185 | var action = menu_action.menu.split(':', 1)[0]; |
||
| 186 | |||
| 187 | if (action === 'copy') { |
||
| 188 | |||
| 189 | API.tabs.query({active: true, currentWindow: true}).then(function (tabs) { |
||
| 190 | var text = login[menu_action.field]; |
||
| 191 | if(menu_action.menu.indexOf('OTP') !== -1){ |
||
| 192 | window.OTP.secret = login.totp; |
||
| 193 | text = window.OTP.getOTP(); |
||
| 194 | } |
||
| 195 | API.tabs.sendMessage(tabs[0].id, {method: "copyTextToClipboard", args: text}); |
||
| 196 | }); |
||
| 197 | return; |
||
| 198 | } |
||
| 199 | |||
| 200 | if (action === 'autoFill') { |
||
| 201 | API.tabs.query({active: true, currentWindow: true}).then(function (tabs) { |
||
| 202 | API.tabs.sendMessage(tabs[0].id, {method: "enterLoginDetails", args: login}); |
||
| 203 | }); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | API.contextMenus.removeAll(); |
||
| 209 | initMenus(); |
||
| 210 | |||
| 211 | return exportContextMenu; |
||
| 212 | |||
| 213 | }()); |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.